 package munyorderweb_Android;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import munyorderweb_DB.DB_manager;

public class GetAllOrderServlet extends HttpServlet
{
//Androidへ注文リストのテーブル情報を送信する //　テーブル別　全メニュー配膳状況
	private final String SQL_ORDER = "select orders.table_no, table_name, SUM(order_delivery) as deli from orders "
			+ "left join customer on orders.customer_no = customer.customer_no "
			+ "left join tables on tables.table_no = orders.table_no "
			+ "where customer.customer_total = 0 group by orders.table_no";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		A_O(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		A_O(req,resp);
	}

	protected void A_O(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		resp.setContentType("text/plain; charset=UTF-8");
		PrintWriter out = resp.getWriter();

		//DB接続
		Connection con = DB_manager.getConnection();

		try {
			Statement stm = con.createStatement();
			//取得した情報を一行の文字列に結合
			ResultSet rs = stm.executeQuery(SQL_ORDER);
			while (rs.next()) {
				out.print(rs.getInt("table_no"));
				out.print(",'");
				out.print(rs.getString("table_name"));
				out.print("',");
				out.print(rs.getInt("deli"));
				out.print("\n");
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		try{
			con.close();
		}catch (SQLException e)
		{
			e.printStackTrace();
		}
	}
}
